<?php
session_start();
include '../comm/config.php';
include '../comm/checkpostandget.php';
include '../comm/msgset.php';

$nonames=array('网站客服');

$tjr = $_SESSION["tjr"];
$name = trim($_POST['username']);
$pwd = trim($_POST['password']);
$md5pwd = md5($pwd);
$email = $_POST['email'];
$qq = $_POST['qq'];
$from = trim($_POST['from']);
$yzm = trim($_POST['yzm']);
$api=$_POST['api']?$_POST['api']:$_GET['api'];

$orther_web = trim($_POST['orther_web']);
$orther_web_id = trim($_POST['orther_web_id']);
$orther_web_name = trim($_POST['orther_web_name']);
if(($yzm==""||$yzm!=$_SESSION["code"])&&$api!='1')
{
	echo "<script language=javascript>window.location='error.php?id=31';</script>";
	exit;
}
if($_GET['orther_web_name'] and $_POST['orther_web_name']=='' and $_GET['apireg']=='auto'){ //当注册方式为第三方自动注册
    $orther_web = trim($_GET['orther_web']);
    $orther_web_id = trim($_GET['orther_web_id']);
    $orther_web_name = trim($_GET['orther_web_name']);
	
	$name=utf_substr($orther_web_name,10);
    $pwd = trim($orther_web_id);
    $md5pwd = md5($pwd);
    $email = $pwd.'@emial.com';
    $qq = 11111;
}


if($orther_web || $orther_web_id || $orther_web_name){
    if (!$orther_web || !$orther_web_id || !$orther_web_name) {
	    echo "缺少必要参数";
	    exit;
    }
}

$sj=date('Y-m-d H:i:s');

if ($name == "" || $pwd == "") {
	echo "<script language=javascript>window.location='error.php?id=0';</script>";
	exit;
}

if(in_array($name,$nonames)){
    echo "<script language=javascript>window.location='error.php?id=21';</script>"; //包含不允许注册的词语
	exit;
}

if (UC == 1) {
	include '../data/config.inc.php';
	include '../uc_client/client.php';

	/////////////////////////////////////
	$uc_name = iconv("utf-8", "utf-8", $name);
	$uid = uc_user_register($uc_name, $pwd, $email);
	/////////////////////////////////////

	if ($uid == -1) {
		echo "<script language=javascript>window.location='error.php?id=22';</script>"; //用户名不合法
		exit;
	}
	elseif ($uid == -2) {
		echo "<script language=javascript>window.location='error.php?id=21';</script>"; //包含不允许注册的词语
		exit;
	}
	elseif ($uid == -3) {
		echo "<script language=javascript>window.location='error.php?id=1';</script>"; //对不起，该用户名已经被注册，请更换一个重新注册
		exit;
	}
	elseif ($uid == -4) {
		echo "<script language=javascript>window.location='error.php?id=25';</script>"; //Email 格式有误
		exit;
	}
	elseif ($uid == -5) {
		echo "<script language=javascript>window.location='error.php?id=26';</script>"; //Email 不允许注册
		exit;
	}
	elseif ($uid == -6) {
		echo "<script language=javascript>window.location='error.php?id=20';</script>"; //该 Email 已经被注册
		exit;
	}
}

$ddlink = mysql_connect($dbserver, $dbuser, $dbpass);
mysql_select_db($dbname);
mysql_query("set names utf8");

if (UC == 0) {
	$qz = "SELECT count(*) FROM " . $BIAOTOU . "user where ddusername='" . $name . "'";
	$rs = mysql_query($qz);
	$t = mysql_fetch_array($rs);
	if ($t[0] > 0) {
		echo "<script language=javascript>window.location='error.php?id=1';</script>"; //对不起，该用户名已经被注册，请更换一个重新注册
		exit;
	}
	$uid = 'NULL';
}

$insert = "INSERT INTO " . $BIAOTOU . "user(Id,ddusername,ddpassword,email,qq,regtime,lastlogintime,loginnum,tjr,pass_question,pass_answer,money,dengji) values ($uid,'$name','$md5pwd','$email','$qq','$sj','$sj',1,'$tjr','$question','$answer','" . ZHUCESONG . "'," . ZHUCESONGLEVEL . ")";
mysql_query($insert);
if (UC == 0) {
	$uid = mysql_insert_id();
}
$tg = round(TGBL / FXBL * 100, 2);
$msg_zhuce = str_replace("[name]", $name, $msg_zhuce);
$msg_zhuce = str_replace("[WEBTITLE]", WEBTITLE, $msg_zhuce);
$msg_zhuce = str_replace("[tg]", $tg, $msg_zhuce);
$insert = "INSERT into " . $BIAOTOU . "msg(ddusername,title,content,addtime,senduser) values ('$name','欢迎注册" . WEBTITLE . "','" . $msg_zhuce . "','" . $sj . "','网站客服')";
mysql_query($insert);
if($orther_web){
    $insert="insert into ".$BIAOTOU."apilogin(dduserid,ortheruserid,ortherusername,ortherwebname,time) values ('$uid','$orther_web_id','$orther_web_name','$orther_web','$sj')";
	mysql_query($insert);
}

if (ZHUCESONG != 0) {
	$insert = "INSERT INTO " . $BIAOTOU . "mingxi(ddusername,shijian,je,addtime,memo) values ('" . $name . "','注册赠送','" . ZHUCESONG . "','" . $sj . "','注册赠送金额" . ZHUCESONG . "元')";

	mysql_query("set names utf8");
	mysql_query($insert);
	$msg_zhucesong = str_replace("[ZHUCESONG]", ZHUCESONG, $msg_zhucesong);
	$insert = "INSERT into " . $BIAOTOU . "msg(ddusername,title,content,addtime,senduser) values ('$name','注册赠送','" . $msg_zhucesong . "','$sj','网站客服')";
	mysql_query($insert);
}

$_SESSION['duoduouser'] = $name;
$_SESSION["dduserid"] = $uid;
$_SESSION["dduserlevel"] = ZHUCESONGLEVEL;
setcookie("ddusername", $name, time() + 1000 * 24 * 60 * 60, "/", SURL);
setcookie("ddpassword", $md5pwd, time() + 1000 * 24 * 60 * 60, "/", SURL);

if($from){
    echo "<script language=javascript>window.location='".$from."';</script>";
}
else{
    echo "<script language=javascript>window.location='center.php';</script>";
}
mysql_close();
?>
